<script setup>
import { debounce } from '@/utils/common.js'

const props = defineProps({
  // 加载效果
  loading: {
    type: Boolean,
    default: false
  },
  // 是否需要防抖
  isDebounce: {
    type: Boolean,
    default: true
  }
})
const emit = defineEmits(['click'])

const onClickBtn = () => {
  // 正在加载中则不触发click事件
  if (!props.loading) {
    emit('click')
  }
}

// 防抖
const onClickDebounce = debounce(onClickBtn, 200)

const onClick = () => {
  props.isDebounce ? onClickDebounce() : onClickBtn()
}
</script>

<template>
  <button class="zy-button" @click="onClick">
    <slot v-if="!loading" />
    <!-- 加载中 -->
    <div class="loading" v-else>
      <svg-icon class="loading-icon" icon-name="loading" />
      <span class="tip">加载中...</span>
    </div>
  </button>
</template>

<style lang="scss" scoped>
.zy-button {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  position: relative;
  cursor: pointer;
  overflow: hidden;
  border: none;
  background: none;
}
.loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.4);
  pointer-events: none;
  .loading-icon {
    font-size: 20px;
    animation: rotate 2s linear infinite;
  }
  .tip {
    margin-left: 5px;
  }
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
